package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * 异常处理
 *
 * @author stone
 * @date 2021/11/24 11:12
 */
//@ControllerAdvice
@RestControllerAdvice
public class ExceptionControllerAdvice {

    //方法的写法和Handler方法是一致的
    /*@ExceptionHandler(ArithmeticException.class)
    public ModelAndView arithmetic() {
        return new ModelAndView("/levelup.jsp");
    }*/

    @ExceptionHandler(ArithmeticException.class)
    public String arithmetic() {
        return "/levelup.jsp";
    }
    //在方法的形参中可以接收你抛出的异常
    @ResponseBody
    @ExceptionHandler(ParameterException.class)
    public BaseRespVo parameter(ParameterException exception) {
        String message = exception.getMessage();
        BaseRespVo fail = BaseRespVo.fail();
        fail.setMsg(message);
        return fail;
    }
}
